
let status_wait = 0, status_loading = 1, status_ok = 2

class Loader
{
    start(task, stepCallback, failCallback)
    {
        this.mTask       = task
        this.mTaskNum    = task.length

        this.mStepCallback     = stepCallback
        this.mFailCallback     = failCallback

        this.mFinishNum  = 0
        this.mError      = 0
		
		this.mStatus     = status_wait
    }

    run()
    {
        if (!this.mTask || this.mError || this.mFinishNum == this.mTaskNum)
        {
            return
        }
		
		if (this.mStatus === status_wait)
		{
            this.statusLoading()

            this.mTask[this.mFinishNum](this)
		}
		else if (this.mStatus === status_ok)
		{
			++this.mFinishNum

            if (this.mStepCallback)
            {
                this.mStepCallback(this.mFinishNum, this.mTaskNum)
            }
			
			this.mStatus = status_wait
		}
    }

    close()
    {
        this.mTask          = null
        this.mStepCallback  = null
        this.mFailCallback  = null
    }

    fail()
    {
        this.mError = 1

        if (this.mFailCallback)
        {
            this.mFailCallback(this.mFinishNum, this.mTaskNum)
        }
    }
	
	statusWait()
	{
		this.mStatus = status_wait
	}
	
	statusLoading()
	{
		this.mStatus = status_loading
	}
	
	statusComplete()
	{
		this.mStatus = status_ok
	}
}

export default Loader
